/**
 * 侦探案件实体类
 * 表示AI侦探应用中的一个案件
 */
export enum CaseStatus {
  PENDING = 'pending',      // 待处理
  IN_PROGRESS = 'in_progress', // 调查中
  SOLVED = 'solved',        // 已解决
  CLOSED = 'closed',        // 已关闭
  COLD = 'cold'            // 冷案
}

export enum CaseDifficulty {
  EASY = 'easy',      // 简单
  MEDIUM = 'medium',  // 中等
  HARD = 'hard',      // 困难
  EXPERT = 'expert'   // 专家
}

export enum CaseCategory {
  MURDER = 'murder',      // 谋杀案
  THEFT = 'theft',        // 盗窃案
  FRAUD = 'fraud',        // 诈骗案
  MISSING = 'missing',    // 失踪案
  ESPIONAGE = 'espionage', // 间谍案
  CYBER = 'cyber'         // 网络犯罪
}

export interface DetectiveCase {
  id: string;
  title: string;
  description: string;
  category: CaseCategory;
  difficulty: CaseDifficulty;
  status: CaseStatus;
  createdAt: string;
  updatedAt: string;
  solvedAt?: string;
  detectiveNotes: string[];
  evidenceList: string[];
  suspectList: string[];
  witnessList: string[];
  aiHints: string[];
  playerScore: number;
  maxPossibleScore: number;
  timeSpent: number; // 秒
  cluesFound: number;
  totalClues: number;
  isNew: boolean;
  isFavorite: boolean;
  tags: string[];
  location: string;
  year: number;
  realWorldCase: boolean; // 是否基于真实案例
  imageUrl?: string;
}

/**
 * 创建新案件的工厂函数
 */
export function createDetectiveCase(data: Partial<DetectiveCase> = {}): DetectiveCase {
  const now = new Date().toISOString();
  
  return {
    id: data.id || generateCaseId(),
    title: data.title || '未命名案件',
    description: data.description || '',
    category: data.category || CaseCategory.MURDER,
    difficulty: data.difficulty || CaseDifficulty.MEDIUM,
    status: data.status || CaseStatus.PENDING,
    createdAt: data.createdAt || now,
    updatedAt: data.updatedAt || now,
    solvedAt: data.solvedAt,
    detectiveNotes: data.detectiveNotes || [],
    evidenceList: data.evidenceList || [],
    suspectList: data.suspectList || [],
    witnessList: data.witnessList || [],
    aiHints: data.aiHints || [],
    playerScore: data.playerScore || 0,
    maxPossibleScore: data.maxPossibleScore || 100,
    timeSpent: data.timeSpent || 0,
    cluesFound: data.cluesFound || 0,
    totalClues: data.totalClues || 10,
    isNew: data.isNew !== undefined ? data.isNew : true,
    isFavorite: data.isFavorite || false,
    tags: data.tags || [],
    location: data.location || '未知地点',
    year: data.year || new Date().getFullYear(),
    realWorldCase: data.realWorldCase || false,
    imageUrl: data.imageUrl,
    ...data
  };
}

/**
 * 生成案件ID
 */
function generateCaseId(): string {
  const timestamp = Date.now();
  const random = Math.random().toString(36).substr(2, 9);
  return `CASE-${timestamp}-${random}`;
}

/**
 * 获取案件状态的中文名称
 */
export function getCaseStatusName(status: CaseStatus): string {
  const statusNames = {
    [CaseStatus.PENDING]: '待处理',
    [CaseStatus.IN_PROGRESS]: '调查中',
    [CaseStatus.SOLVED]: '已解决',
    [CaseStatus.CLOSED]: '已关闭',
    [CaseStatus.COLD]: '冷案'
  };
  return statusNames[status];
}

/**
 * 获取案件难度的中文名称
 */
export function getCaseDifficultyName(difficulty: CaseDifficulty): string {
  const difficultyNames = {
    [CaseDifficulty.EASY]: '简单',
    [CaseDifficulty.MEDIUM]: '中等',
    [CaseDifficulty.HARD]: '困难',
    [CaseDifficulty.EXPERT]: '专家'
  };
  return difficultyNames[difficulty];
}

/**
 * 获取案件分类的中文名称
 */
export function getCaseCategoryName(category: CaseCategory): string {
  const categoryNames = {
    [CaseCategory.MURDER]: '谋杀案',
    [CaseCategory.THEFT]: '盗窃案',
    [CaseCategory.FRAUD]: '诈骗案',
    [CaseCategory.MISSING]: '失踪案',
    [CaseCategory.ESPIONAGE]: '间谍案',
    [CaseCategory.CYBER]: '网络犯罪'
  };
  return categoryNames[category];
}

/**
 * 计算案件完成度百分比
 */
export function getCaseCompletionPercentage(caseItem: DetectiveCase): number {
  if (caseItem.status === CaseStatus.SOLVED) {
    return 100;
  }
  
  const cluesProgress = caseItem.totalClues > 0 ? (caseItem.cluesFound / caseItem.totalClues) * 100 : 0;
  const scoreProgress = caseItem.maxPossibleScore > 0 ? (caseItem.playerScore / caseItem.maxPossibleScore) * 100 : 0;
  
  return Math.min(Math.round((cluesProgress + scoreProgress) / 2), 99);
}